package com.phpxiu.app.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.phpxiu.app.adapter.holder.FriendPickerViewHolder;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.list.WatchFriend;
import com.phpxiu.app.utils.KKYUtil;
import com.phpxiu.app.view.custom.ListEmptyView;

import java.util.List;

/**
 * Created by 吴金洲 on 2016/8/4
 */
public class FriendPickerAdapter extends BaseAdapter<WatchFriend> {
    private WatchFriend empty=new WatchFriend();
    private ListEmptyView emptyView;

    public FriendPickerAdapter(Context context, List<WatchFriend> data) {
        super(context, data);
        empty.setListViewType(WatchFriend.ITEM_EMPTY);
        data.add(0,empty);
        emptyView=new ListEmptyView(context);
        emptyView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setMsg(context.getString(R.string.list_empty_title));
        emptyView.setPadding(0, KKYUtil.dip2px(context,75), 0, 0);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        WatchFriend friend=data.get(position);
        if(friend.getListViewType()==WatchFriend.ITEM_EMPTY){
            convertView=emptyView;
        }else{
            FriendPickerViewHolder holder=null;
            if(convertView==null||convertView==emptyView){
                convertView=mInflater.inflate(R.layout.friend_picker_list_item_view,null);
                holder=new FriendPickerViewHolder(convertView);
                convertView.setTag(holder);
            }
            holder=(FriendPickerViewHolder)convertView.getTag();
            holder.setFriend(friend);
            holder.headView.setImageURI(Uri.parse(friend.getAvatar()));
            holder.nickView.setText(friend.getNickname());
            String sex=friend.getSex();
            if("1".equals(sex)){
                holder.sexView.setImageResource(R.mipmap.user_sex_icon_male);
            }else{
                holder.sexView.setImageResource(R.mipmap.user_sex_icon_female);
            }
            if(!"".equals(friend.getLvl())){
                //holder.lv.setVisibility(View.VISIBLE);
                //holder.lv.setImageURI(Uri.parse("asset:///lvIcons/" + friend.getLvl() + ".png"));
            }else{
                //holder.lv.setVisibility(View.INVISIBLE);
            }
            holder.setIntroViewText(friend.getIntro());
            if(friend.isSelected()){
                if(holder.checkBtn.getVisibility()!=View.VISIBLE){
                    holder.checkBtn.setVisibility(View.VISIBLE);
                }
            }else{
                if(holder.checkBtn.getVisibility()==View.VISIBLE){
                    holder.checkBtn.setVisibility(View.INVISIBLE);
                }
            }
            holder.showDivider();
            if(data.size()>0&&position==data.size()-1){
                holder.hideDivider();
            }
        }
        return convertView;
    }

    /**
     * 刷新列表
     */
    public void refresh() {
        if(emptyView!=null){
            emptyView.loaded();
        }
        if(data.size()>1){
            data.remove(empty);
        }else if(data.size()==0){
            data.add(empty);
        }
        super.notifyDataSetChanged();
    }
}
